package com.example.graphicslib.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

import com.example.graphicslib.animations.Animation;
import com.example.graphicslib.animations.BaseFrameAnimation;

import java.io.IOException;

/**
 * Created by Tatev on 13/4/2014.
 */
public class FrameAnimationExampleView extends View {

    public FrameAnimationExampleView(Context context) {
        super(context);
        this.context = context;

        paint = new Paint();
        paint.setColor(Color.BLACK);
        fps = 10;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(frames[frameAnimation.getCurrentFrame()], width / 3, height / 3, null);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        width = w;
        height = h;

        init();
    }

    private void init() {

        frames = new Bitmap[6];

        try {
            for(int i = 0; i < frames.length; ++i){
                frames[i] = Bitmap.createScaledBitmap(BitmapFactory.decodeStream(context.getAssets().open("img" + (i + 1) + ".png")),
                        width / 6, width / 6, true);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        frameAnimation = new BaseFrameAnimation(frames.length, 5000);
        frameAnimation.setFps(fps);

        animation = new Animation(1000 / fps, frameAnimation, this);
        animation.start();
    }

    private Context context;
    private Bitmap[] frames;
    private Animation animation;
    private BaseFrameAnimation frameAnimation;
    private Paint paint;
    private int fps;
    private int width, height;
}
